package com.hgnu.dorm.service.impl;

import com.hgnu.dorm.domain.dto.DormitoryPageQueryDto;
import com.hgnu.dorm.domain.po.Building;
import com.hgnu.dorm.domain.po.Dormitory;
import com.hgnu.dorm.domain.po.PageBean;
import com.hgnu.dorm.domain.vo.DormitoryVo;
import com.hgnu.dorm.exception.BaseException;
import com.hgnu.dorm.mapper.BuildingMapper;
import com.hgnu.dorm.mapper.DormitoryMapper;
import com.hgnu.dorm.service.DormitoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DormitoryServiceImpl implements DormitoryService {
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Override
    public PageBean page(DormitoryPageQueryDto dto) {
        Long total = dormitoryMapper.count(dto);
        if(dto.getPage()<=0){
            dto.setPage(1);
        }
        if(dto.getPageSize()<=0){
            dto.setPageSize(10);
        }
        Integer start = (dto.getPage() - 1) * dto.getPageSize();
        dto.setStart(start);
        List<Dormitory> page = dormitoryMapper.page(dto);
        List<DormitoryVo> dormitoryvo = page.stream().map(item -> {
            DormitoryVo vo = new DormitoryVo();
            BeanUtils.copyProperties(item, vo);
            Integer id = item.getBuildingId();
            vo.setBuildingName(buildingMapper.getById(id).getName());
            return vo;
        }).collect(Collectors.toList());
        return new PageBean(total, dormitoryvo);
    }
    /**
     * 查询宿舍信息
     *
     * @param id 宿舍信息主键
     * @return 宿舍信息
     */
    @Override
    public DormitoryVo selectDormitoryById(Integer id)
    {
        Dormitory dormitory = dormitoryMapper.selectDormitoryById(id);
        Building building = buildingMapper.getById(dormitory.getBuildingId());
        DormitoryVo vo = new DormitoryVo();
        BeanUtils.copyProperties(dormitory,vo);
        vo.setBuildingName(building.getName());
        return vo;
    }

    /**
     * 新增宿舍信息
     *
     * @param dormitory 宿舍信息
     * @return 结果
     */
    @Override
    public int insertDormitory(Dormitory dormitory)
    {
        Integer maxCapacity = dormitory.getMaxCapacity();
        if(maxCapacity == null){
            maxCapacity = 10;
            dormitory.setMaxCapacity(maxCapacity);
        }
        dormitory.setCurrentOccupancy(0);
        dormitory.setCreateTime(LocalDateTime.now());
        return dormitoryMapper.insertDormitory(dormitory);
    }

    /**
     * 修改宿舍信息
     *
     * @param dormitory 宿舍信息
     * @return 结果
     */
    @Override
    public int updateDormitory(Dormitory dormitory)
    {

        Integer maxCapacity = dormitory.getMaxCapacity();
        Integer id = dormitory.getId();
        if(maxCapacity == null){
            maxCapacity = 10;
            dormitory.setMaxCapacity(maxCapacity);
        }
        Dormitory oldDorm = dormitoryMapper.selectDormitoryById(id);
        Integer currentOccupancy = oldDorm.getCurrentOccupancy();
        if(maxCapacity < currentOccupancy){
            throw new BaseException("最大可住人数小于已住人数");
        }
        dormitory.setUpdateTime(LocalDateTime.now());
        return dormitoryMapper.updateDormitory(dormitory);
    }

    /**
     * 批量删除宿舍信息
     *
     * @param ids 需要删除的宿舍信息主键
     * @return 结果
     */
    @Override
    public int deleteDormitoryByIds(Long[] ids)
    {
        int countStatus = dormitoryMapper.countStatus(ids);
        if(countStatus>0){
            throw new BaseException("无法删除已住有学生的宿舍");
        }
        return dormitoryMapper.deleteDormitoryByIds(ids);
    }

    /**
     * 获取可选择宿舍楼信息
     */
    @Override
    public List<DormitoryVo> select() {
        List<Dormitory> dormitories = dormitoryMapper.selectDormitoryList();
        List<DormitoryVo> vos = dormitories.stream().map(item -> {
            DormitoryVo vo = new DormitoryVo();
            BeanUtils.copyProperties(item, vo);
            vo.setBuildingName(buildingMapper.getById(item.getBuildingId()).getName());
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }
}
